<template>
    <div>
        <div class="bg-banner" />
        <div id="login-box">
            <!--<div class="login-banner" />-->
            <el-form ref="form" :model="form" :rules="rules" class="login-form" auto-complete="on" label-position="left">
                <div class="title-container">
                    <h3 class="title">{{ title }}</h3>
                </div>
                <div>
                    <el-form-item prop="role">
                        <el-radio v-model="form.role" label="老师">老师</el-radio>
                        <el-radio v-model="form.role" label="学生">学生</el-radio>
                    </el-form-item>
                    <el-form-item prop="account">
                        <el-input ref="name" v-model="form.account" placeholder="用户名" type="text" tabindex="1" auto-complete="on">
                            <svg-icon slot="prefix" name="user" />
                        </el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input ref="password" v-model="form.password" :type="passwordType" placeholder="密码" tabindex="2" auto-complete="on" @keyup.enter.native="handleLogin">
                            <svg-icon slot="prefix" name="password" />
                            <svg-icon slot="suffix" :name="passwordType === 'password' ? 'eye' : 'eye-open'" @click="showPassword" />
                        </el-input>
                    </el-form-item>
                    <el-form-item prop="sex">
                        <el-radio v-model="form.sex" label="男">男</el-radio>
                        <el-radio v-model="form.sex" label="女">女</el-radio>
                    </el-form-item>
                </div>
                <el-button :loading="loading" type="primary" style="width: 100%;" @click.native.prevent="handleRegistry">注 册</el-button>
            </el-form>
        </div>
        <Copyright v-if="$store.state.settings.showCopyright" />
    </div>
</template>

<script>
export default {
    name: 'Registry',
    data() {
        return {
            title: process.env.VUE_APP_TITLE,
            form: {
                role: '',
                account: '',
                password: '',
                sex: '',
                remember: !!localStorage.login_account
            },
            rules: {
                account: [
                    { required: true, trigger: 'blur', message: '请输入用户名' }
                ],
                password: [
                    { required: true, trigger: 'blur', message: '请输入密码' },
                    { min: 6, max: 18, trigger: 'blur', message: '密码长度为6到18位' }
                ]
            },
            // 是否加载中状态
            loading: false,
            // 这个判断密码是否可见
            passwordType: 'password',
            redirect: undefined
        }
    },
    // 监听函数
    watch: {
        $route: {
            handler: function(route) {
                this.redirect = route.query && route.query.redirect
            },
            immediate: true
        }
    },
    methods: {
        showPassword() {
            this.passwordType = this.passwordType === 'password' ? '' : 'password'
            this.$nextTick(() => {
                this.$refs.password.focus()
            })
        },
        handleRegistry() {
            // this.$refs.form.validate(valid => {
            //     if (valid) {
            //         // loading是那个加载中转圈圈的状态，true为显示，false为不显示
            //         this.loading = true
            //         // dispatch用来触发Vuex的Store里的Action的函数，第一个参数是指定哪个Action函数，第二个开始的参数则要和函数里的参数对应
            //         this.$store.dispatch('user/login', this.form).then(() => {
            //
            //             this.loading = false
            //             this.form.remember && localStorage.setItem('login_account', this.form.account)
            //             this.$router.push({ path: this.redirect || '/' })
            //
            //         }).catch(() => {
            //             this.loading = false
            //         })
            //     }
            // })
            this.$router.push('/login')
        },
        testAccount(account) {
            this.form.account = account
            this.form.password = '123456'
            this.handleLogin()
        }
    }
}
</script>

<style lang="scss" scoped>
[data-mode=mobile] {
    #login-box {
        max-width: 80%;
        .login-banner {
            display: none;
        }
    }
}
::v-deep input[type=password]::-ms-reveal {
    display: none;
}
.bg-banner {
    position: absolute;
    z-index: 0;
    width: 100%;
    height: 100%;
    //登录背景图片URL
    background-image: url(../assets/images/bg.jpg);
    background-size: cover;
    background-position: center center;
}
#login-box {
    display: flex;
    justify-content: space-between;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translateX(-50%) translateY(-50%);
    background: rgba(255, 255, 255, 0.8);
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 0 5px #999;
    //登录表单左边面板展示，类似WeGame登录面板左边展示效果一样
    .login-banner {
        width: 300px;
        //登录表单面板图片URL
        background-image: url(../assets/images/login-banner.jpg);
        background-size: cover;
        background-position: center center;
    }
    .login-form {
        width: 450px;
        padding: 50px 35px 30px;
        overflow: hidden;
        .svg-icon {
            vertical-align: -0.35em;
        }
        .title-container {
            position: relative;
            .title {
                font-size: 22px;
                color: #666;
                margin: 0 auto 30px;
                text-align: center;
                font-weight: bold;
                @include text-overflow;
            }
        }
    }
    ::v-deep .el-input {
        display: inline-block;
        height: 48px;
        width: 100%;
        input {
            height: 48px;
        }
        .el-input__prefix {
            left: 10px;
        }
        .el-input__suffix {
            right: 10px;
        }
    }
    .el-checkbox {
        margin: 20px 0;
    }
}
.copyright {
    position: absolute;
    bottom: 30px;
    width: 100%;
    margin: 0;
    mix-blend-mode: difference;
}
</style>
